package com.lewis.anf;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.lewis.anf.pojo.AnfBaseItem;
import com.lewis.anf.pojo.AnfCat;
import com.lewis.anf.pojo.AnfCategory;
import com.lewis.anf.pojo.AnfUrlsPair;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.Annotations;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class ProbeStart {
	
	private static String baseUrl = "http://www.abercrombie.com/shop/us/womens-featured-items-clearance";

	public static String probe() {
		
		//proxy setting
//		HttpHost proxy = new HttpHost("connsvr.nike.com",8080);
		HttpClient httpClient = new DefaultHttpClient();
		
//		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
		
		HttpUriRequest httpUriRequest = new HttpGet(baseUrl);
		
		HttpResponse httpResponse = null;
		try {
			httpResponse = httpClient.execute(httpUriRequest);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		HttpEntity entity = httpResponse.getEntity();
		String html = null;
		try {
			html = new String(EntityUtils.toString(entity).getBytes());
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Document doc = Jsoup.parse(html);
		Element listUrls = doc.getElementsByClass("secondary").get(0);
		Elements ulList = listUrls.getElementsByTag("a");
		
		AnfCat cats = new AnfCat();
		for (Element e:ulList){
//			subUrls.put(e.text(), "http://www.abercrombie.com" + e.attr("href").toString());
			String name = e.text();
			if (name.equals("Featured Items")){
				continue;
			}
			AnfCategory cat = new AnfCategory();
			cats.getList().add(cat);
			cat.setName(name);
			String subUrl = "http://www.abercrombie.com" + e.attr("href").toString();
			try {
				httpUriRequest = new HttpGet(subUrl);
				httpResponse = httpClient.execute(httpUriRequest);
				entity = httpResponse.getEntity();
				html = new String(EntityUtils.toString(entity).getBytes());
			} catch (ClientProtocolException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			//parse sub url
			Document subDoc = Jsoup.parse(html);
			Elements subProducts = subDoc.getElementsByClass("product-wrap");
			
			List<AnfBaseItem> items = new ArrayList<AnfBaseItem>();
			cat.setItems(items);
			
			for(Element subProduct:subProducts){
				AnfBaseItem item = new AnfBaseItem();
				AnfUrlsPair urlPair = new AnfUrlsPair();
				Element a = subProduct.getElementsByTag("a").get(0);
				item.setProdId(a.attr("data-productid").toString());
				urlPair.setItemUrl("http://www.abercrombie.com"+ a.attr("href").toString());
				urlPair.setPicUrl("http:" + subProduct.getElementsByTag("img").get(0).attr("data-src").toString());
				item.getUrls().add(urlPair);
				Element nameUrl = subProduct.getElementsByTag("h2").get(0);
				item.setTitle(nameUrl.getElementsByTag("a").get(0).text());
				item.setDesc(subProduct.getElementsByClass("desc").get(0).text());
				String retailPrice = subProduct.getElementsByClass("list-price").get(0).text();
				String offerPrice = subProduct.getElementsByClass("offer-price").get(0).text();
				item.setOriginPrice(retailPrice);
				item.setDiscountPrice(offerPrice);
				
				
				String defaultSeq = a.attr("data-seq").toString();
				
				//determine if it's got multiple color
				Elements colorSwitch = subProduct.getElementsByClass("swatches");
				if (!colorSwitch.isEmpty()){
					Elements list = colorSwitch.get(0).getElementsByTag("a");
					for (Element color: list){
						String currentSeq = color.attr("data-seq").toString();
						if (currentSeq.equals(defaultSeq)){
							continue;
						}
						AnfUrlsPair itemUrlPair = new AnfUrlsPair();
						itemUrlPair.setItemUrl("http://www.abercrombie.com" + color.attr("href").toString());
						String seq = "_" + color.attr("data-seq").toString() + "_";
						String defaultUrl = urlPair.getPicUrl();
						itemUrlPair.setPicUrl(defaultUrl.replaceAll("_"+defaultSeq+"_", seq));
						item.getUrls().add(itemUrlPair);
					}
				}
				System.out.println(item);
				items.add(item);
	
			}
			
		}
		//to xml
        XStream xstream = new XStream(new DomDriver());
        Annotations.configureAliases(xstream, AnfCat.class);
        Annotations.configureAliases(xstream, AnfCategory.class);
        Annotations.configureAliases(xstream, AnfBaseItem.class);
        Annotations.configureAliases(xstream, AnfUrlsPair.class);
        String obj_xml=xstream.toXML(cats);
        
        return obj_xml;
		
	}
	 public static void main(String[] args) {
		probe();
	}
}
